package top.tangyh.lamp.care.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.tangyh.basic.base.R;
import top.tangyh.basic.interfaces.echo.EchoService;
import top.tangyh.basic.base.controller.SuperController;
import top.tangyh.lamp.care.service.BCareRecordService;
import top.tangyh.lamp.care.service.BDocRecordService;
import top.tangyh.lamp.care.entity.BDocRecord;
import top.tangyh.lamp.care.vo.result.FanShenRecordOutputDTO;
import top.tangyh.lamp.care.vo.result.PageOutputDTO;
import top.tangyh.lamp.care.vo.save.BDocRecordSaveVO;
import top.tangyh.lamp.care.vo.save.FanShenRecordInputDTO;
import top.tangyh.lamp.care.vo.update.BDocRecordUpdateVO;
import top.tangyh.lamp.care.vo.result.BDocRecordResultVO;
import top.tangyh.lamp.care.vo.query.BDocRecordPageQuery;
import top.tangyh.lamp.common.utils.ConvertUtil;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 文书记录表
 * </p>
 *
 * @author zqb
 * @date 2024-07-29 14:16:16
 * @create [2024-07-29 14:16:16] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/doc")
@Tag(name = "系统：文书")
public class BDocRecordController {
    //    extends SuperController<BDocRecordService, Long, BDocRecord
//            , BDocRecordSaveVO, BDocRecordUpdateVO, BDocRecordPageQuery, BDocRecordResultVO>
//    private final EchoService echoService;
//    @Override
//    public EchoService getEchoService() {
//        return echoService;
//    }

    private final BDocRecordService bDocRecordService;


    private final BCareRecordService bCareRecordService;

    @Operation(summary = "新增文书")
    @PostMapping("/add")
    public R add(@RequestBody BDocRecordSaveVO yxDocRecordVO) {
        bDocRecordService.doSave(yxDocRecordVO);

        return R.success();
    }

    @Operation(summary = "更新文书")
    @PutMapping("update/carePgRecord")
    public R updateById(@Parameter(description = "文书对象") @Valid @RequestBody BDocRecordSaveVO carePgRecordVo) {
        bDocRecordService.doSave(carePgRecordVo);
        return R.success();
    }

    @Operation(summary = "根据ID查询文书详情")
    @GetMapping(value = "/docRecord/{id}")
    public R<BDocRecordResultVO> getDocRecord(@PathVariable("id") Long id) {
        BDocRecord po = bDocRecordService.getById(id);
        if (null != po) {
            return R.success(ConvertUtil.convert(po, BDocRecordResultVO.class));
        } else {
            return R.success(null);
        }
    }

    @Operation(summary = "根据ID删除文书")
    @DeleteMapping(value = "")
    public R deleteReocrd(@Parameter(description = "表单id", required = true) @RequestParam("id") Long id) {
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        boolean result = bDocRecordService.removeByIds(ids);
        if (result == true) {
            return R.success();
        } else {
            return R.fail("删除失败");
        }
    }

    /**
     * 查询文书记录列表 按照type分组
     */
    @Operation(summary = "查询文书记录列表")
    @GetMapping(value = "/list")
    public R<List<BDocRecordResultVO>> list(@Parameter(description = "病人识别号", required = false) @RequestParam(value = "patientIdent", required = false) String patientIdent,
                                            @Parameter(description = "表单类型", required = false) @RequestParam(value = "type", required = false) Integer type,
                                            @Parameter(description = "病区", required = false) @RequestParam(value = "wardIdent", required = false) String wardIdent,
                                            @Parameter(description = "起始（记录时间）(入科时间)", required = false) @RequestParam(value = "startTime", required = false) Long startTime,
                                            @Parameter(description = "结束（记录时间）(出科时间)", required = false) @RequestParam(value = "endTime", required = false) Long endTime) {
        return R.success(bDocRecordService.queryAll(patientIdent, type, wardIdent, startTime, endTime));
    }

    @Operation(summary = "更具病人id和类型，查询文书表单详情")
    @GetMapping(value = "/detail/listone")
    public R<BDocRecordResultVO> getDocRecord(@Parameter(description = "病人识别号", required = false) @RequestParam(value = "patientIdent", required = false) String patientIdent,
                                              @Parameter(description = "表单类型", required = false) @RequestParam(value = "type", required = false) Integer type
    ) {
        return R.success(bDocRecordService.detialListOne(patientIdent, type));

    }

    @GetMapping("/fanshen/page")
    @Operation(description = "翻身记录单-分页查询翻身记录操作")
    public R<PageOutputDTO<FanShenRecordOutputDTO>> queryFanShenRecord(FanShenRecordInputDTO fanShenRecordInputDTO) {
        return R.success(bCareRecordService.queryFanShenRecord(fanShenRecordInputDTO));
    }

    @GetMapping("/fanshen/all")
    @Operation(description = "翻身记录单-查询全部翻身记录操作")
    public R<List<FanShenRecordOutputDTO>> queryAllFanShenRecord(FanShenRecordInputDTO fanShenRecordInputDTO) {
        return R.success(bCareRecordService.queryAllFanShenRecord(fanShenRecordInputDTO));
    }


}


